package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class TableLayoutSlice extends AbilitySlice {

    private Image back_button, tab_dropdown, tab_image_one, tab_image_two;
    private Text text_title, text_one, text_two, text_three, text_dialog;
    private DirectionalLayout dialog_lin, tab_layout, samsungLayout;
    private DependentLayout dialog_depen;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_table_layout);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("TableLayout")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("TableLayout")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("TableLayout", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("TableLayout", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("TableLayout", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        dialog_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_dialog_lin);
        tab_dropdown = (Image) findComponentById(ResourceTable.Id_tab_image_dropdown);
        tab_dropdown.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_dropdown));
        tab_image_one = (Image) findComponentById(ResourceTable.Id_tab_image_one);
        tab_image_one.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_prev));
        tab_image_two = (Image) findComponentById(ResourceTable.Id_tab_image_two);
        tab_image_two.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_next));
        dialog_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_dialog_lin);
        tab_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_tab_layout);
        text_one = (Text) findComponentById(ResourceTable.Id_text_one);
        text_two = (Text) findComponentById(ResourceTable.Id_text_two);
        text_three = (Text) findComponentById(ResourceTable.Id_text_three);
        text_dialog = (Text) findComponentById(ResourceTable.Id_text_dialog);

        text_one.setClickedListener(component -> {
            text_dialog.setText(10 + "");
            dialog_depen.setVisibility(Component.HIDE);
        });
        text_two.setClickedListener(component -> {
            text_dialog.setText(20 + "");
            dialog_depen.setVisibility(Component.HIDE);
        });
        text_three.setClickedListener(component -> {
            text_dialog.setText(50 + "");
            dialog_depen.setVisibility(Component.HIDE);
        });
        dialog_depen = (DependentLayout) findComponentById(ResourceTable.Id_dialog_depen);
        dialog_lin.setClickedListener(component -> dialog_depen.setVisibility(Component.VISIBLE));
        tab_layout.setClickedListener(component -> dialog_depen.setVisibility(Component.INVISIBLE));
        text_title.setText("TableLayout");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
